<?php


namespace app\api\controller;


use app\api\model\System\SysSettingModel;
use app\api\model\User\UserModel;
use app\Utils\Util;
use think\App;

class BaseController extends \app\BaseController
{

    const CODE_SUCCESS = 0;
    const CODE_ERROR = -1;
    const CODE_LOGIN = -2;
    protected $setting;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->setting = SysSettingModel::getSysSettingWhere([['id','=',1]]);
    }


    public function getInviteCode()
    {
        $invite_code = Util::randomCode(8,4);
        $user = UserModel::getUserWhere([['invite_code','=',$invite_code]]);
        if(!empty($user)){
            $invite_code = $this->getCode();
        }
        return $invite_code;
    }

    /**
     * 常用于post参数校验 如: post请求需要a, b, c, d 四个必传参数 $this->paramRequire(['a','b','c','d'], $postParam)
     * @param $require
     * @param $param
     * @return string
     */
    public function paramRequire($require,$param){
        $keys = array_values($require);
        foreach ($keys as $v){
            if (!isset($param[$v])){
                return (string)$v;
            }
        }
        return '';
    }

    /**
     * 常用于 post参数赋值到一个数组中
     * @param $receive
     * @param $keys
     * @param $value
     */
    public function paramAssign(&$receive,$keys,$value){
        foreach ($keys as $v){
            if (isset($value[$v])){
                $receive[$v] = $value[$v];
            }
        }
    }

}
